//detail模块的小仓库
//引入接口文件
import { reqGoodsInfo, resAddOrUpdateShopCar } from '@/api'
//引入封装游客身份的模块
import { getUuid } from '@/utils/uuid_token'

const state = {
    goodsList: {},
    uuid_token: getUuid()
};

const actions = {
    //通过api里的接口函数调用，向服务器发请求，获取服务器的数据
    async goodsList({ commit }, skuId) {
        let res = await reqGoodsInfo(skuId);
        if (res.code == 200) {
            commit('GOODSLIST', res.data)
        }
    },
    async addOrUpdateShopCar({ commit }, { skuId, skuNum }) {
        let res = await resAddOrUpdateShopCar(skuId, skuNum);
        if (res.code == 200) {
            return 'success'
        } else {
            return Promise.reject(new Error('addOrUpdateShopCar failed'))
        }
    }
};

const mutations = {
    GOODSLIST(state, goodsList) {
        state.goodsList = goodsList;
    },
};

const getters = {
    categoryView(state) {
        return state.goodsList.categoryView || {};
    },
    skuInfo(state) {
        return state.goodsList.skuInfo || {};
    },
    skuAttrValueList(state) {
        return state.goodsList.skuInfo.skuAttrValueList || [];
    },
    skuSaleAttrValueList(state) {
        return state.goodsList.skuInfo.skuSaleAttrValueList || [];
    },
    spuSaleAttrList(state) {
        return state.goodsList.spuSaleAttrList || [];
    }
};

export default {
    state,
    mutations,
    actions,
    getters
}